<?php
require_once dirname(__FILE__) . '/config.php';
require_once 'functions/sql.php';
$sql = new sql();
$sql->connect();
@session_start();
?>
<?php

$QBIDEMAILArray = Array();

// Set up the IPP instance
$IPP = new QuickBooks_IPP($dsn);

// Get our OAuth credentials from the database
$creds = $IntuitAnywhere->load($the_username, $the_tenant);

// Tell the framework to load some data from the OAuth store
$IPP->authMode(
	QuickBooks_IPP::AUTHMODE_OAUTH, 
	$the_username, 
	$creds);

$realm = $creds['qb_realm'];
// Load the OAuth information from the database
if ($Context = $IPP->context())
{
	// Set the IPP version to v3 
	$IPP->version(QuickBooks_IPP_IDS::VERSION_3);
	$CustomerService = new QuickBooks_IPP_Service_Customer();
	$Customer = new QuickBooks_IPP_Object_Customer();
	//check user in qb
	
	$display_name = $arraydata['user_firstname']." ".$arraydata['user_lastname'];
	$display_name = trim($display_name);
	//$display_name =  'sachil sawhney';
	//$display_name =  'james james - 4';
	$customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer where DisplayName= '".$display_name."'");
	
			
        
	if(count($customers) > 0){
            $customerslike = $CustomerService->query($Context, $realm, "SELECT DisplayName FROM Customer where DisplayName LIKE '%".$display_name." - %'");
            $displaycount = count($customerslike) + 1;
            $display_name  = $display_name . " - ".$displaycount;
	}
        
       
        
            $Customer->setTitle($arraydata['user_prefix']);
            $Customer->setGivenName($arraydata['user_firstname']);
            //$Customer->setMiddleName('B');
            $Customer->setFamilyName($arraydata['user_lastname']);
            //$Customer->setDisplayName($arraydata['user_firstname']." ".$arraydata['user_lastname']." - ".$lastid);
            $Customer->setDisplayName($display_name);
            $Customer->setSalesTermRef(4);
            // Phone #
            $PrimaryPhone = new QuickBooks_IPP_Object_PrimaryPhone();
            $PrimaryPhone->setFreeFormNumber($arraydata['user_phoneno']);
            $Customer->setPrimaryPhone($PrimaryPhone);
            // Mobile #
            $Mobile = new QuickBooks_IPP_Object_Mobile();
            $Mobile->setFreeFormNumber($arraydata['user_phoneno']);
            $Customer->setMobile($Mobile);
            // Bill address
            $BillAddr = new QuickBooks_IPP_Object_BillAddr();
            $BillAddr->setLine1($arraydata['user_address']);
            $BillAddr->setCity($arraydata['user_city']);
            $BillAddr->setCountry($arraydata['user_country']);
            $BillAddr->setCountrySubDivisionCode($arraydata['user_state']);
            $BillAddr->setPostalCode($arraydata['user_zipcode']);
            $Customer->setBillAddr($BillAddr);

		// Email
		$PrimaryEmailAddr = new QuickBooks_IPP_Object_PrimaryEmailAddr();
		$PrimaryEmailAddr->setAddress($arraydata['user_email']);
		$Customer->setPrimaryEmailAddr($PrimaryEmailAddr);

              /*  
                $customers_chkallData = $CustomerService->query($Context, $realm, "SELECT * FROM Customer");
				$status = 0;
				if(count($customers_chkallData) > 0){
					foreach ($customers_chkallData as $Customer1)
					{
						
						if ($Customer1->getPrimaryEmailAddr()) {
								$QBIDEMAILArray[$Customer1->getId()] = $Customer1->getPrimaryEmailAddr()->getAddress();
						}
					   
					}
			
                
					foreach ($QBIDEMAILArray as $key => $value)
					{
						if($arraydata['user_email'] == $value){
							$quickbook_id = $key;
							$status = 1;
						}
					}
				}
				*/
			
			    $customers_chkallData = $CustomerService->query($Context, $realm, "SELECT * FROM Customer where PrimaryEmailAddr = '".$arraydata['user_email']."'");
				
				$status = 0;
				if(count($customers_chkallData) > 0){
					foreach ($customers_chkallData as $Customer1)
					{
						$quickbook_id = $Customer1->getId();
						$status = 1;
					}
				}
				
				
                if($status == 0){
                    if ($resp = $CustomerService->add($Context, $realm, $Customer))
                    {
                        $quickbook_id = $resp;
                    }else
                    {
                        print($CustomerService->lastError($Context));
                    }

                }
           $_SESSION['CHECKOUT_DATA']['qb_customer_id'] = $quickbook_id;
}
else
{
	die('Unable to load a context...?');
}
?>
